<?php
/**
 * Hoffman Framework
 *
 * LICENSE
 *
 * This source file is subject to the GNU LGPL 2.1 license that is bundled
 * with this package in the file license.txt.
 * It is also available through the world-wide-web at this URL:
 * http://creativecommons.org/licenses/LGPL/2.1
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to annop@thaicyberpoint.com so we can send you a copy immediately.
 *
 * @package    Hoffman
 * @author     Ford AntiTrust
 * @copyright  Copyright (c) 2008 Ford AntiTrust (http://www.thaicyberpoint.com/ford/blog)
 * @since      Version Rv 0.2
 * @license    http://creativecommons.org/licenses/LGPL/2.1
 *             GNU LGPL 2.1
 * @filesource
 */

if (!defined('INDEX'))
    die('No direct script access allowed.');

/**
 * @see Zend_Locale
 */
require_once 'Zend/Locale.php';
/**
 * @see Zend_Translate
 */
require_once 'Zend/Translate.php';
/**
 * @see Hmf_Errors
 */
require_once 'Hmf/Errors.php';

/**
 * Hmf_Translate
 *
 * @package     Hoffman
 * @subpackage  Hmf
 * @category    Translate
 * @author      Ford AntiTrust
 * @version     SVN: $Id: Translate.php 167 2010-02-18 10:32:16Z annop.s@gmail.com $
 * @link        http://www.thaicyberpoint.com/ford/blog/hmf
 */

class Hmf_Translate {

    const FILE_EXT = '.mo';

    private static $translate;

    private static $nowAvailable = false;

    private $type = 'application';

    public function change(Zend_Locale $locale, $type = 'application', array $use = array()) {

        /*
         *
         * Application      <- application/language/<language>.mo
         * Component        <- application/language/component/<component>/<language>.mo
         * Controller       <- application/language/controller/<controller>/<language>.mo
         * Action           <- application/language/controller/<controller>/<action>/<language>.mo
         *
         */

        $language = $locale->getLanguage();

        if (in_array($type, array('application', 'component', 'controller', 'action'))) {
            $this->type = $type;
        } else {
            return false;
        }

        $path = null;

        switch ($this->type) {

            case 'application' :
                $path = PATH_DIR_APPLICATION_LANGUAGE . '/' . $language . self::FILE_EXT;
                break;
            case 'component' :
                $path = PATH_DIR_APPLICATION_LANGUAGE_COMPONENT . '/' . $use['component'] . '/' . $language . self::FILE_EXT;
                break;
            case 'controller' :
                $path = PATH_DIR_APPLICATION_LANGUAGE_CONTROLLER . '/' . $use['controller'] . '/' . $language . self::FILE_EXT;
                break;
            case 'action' :
                $path = PATH_DIR_APPLICATION_LANGUAGE_CONTROLLER . '/' . $use['controller'] . '/' . $use['action'] . '/' . $language . self::FILE_EXT;
                break;

        }

        try {
            if (file_exists($path)) {
                if (isset(self::$translate) and self::$translate instanceof Zend_Translate) {
                    self::$translate->addTranslation($path, $locale);
                } else {
                    self::$nowAvailable = true;
                    self::$translate = new Zend_Translate('gettext', $path, $locale);
                }
                return true;
            }
        } catch (Exception $e) {
            // File not found, no adapter class...
            // General failure
            Hmf_Errors::showError($e->getCode(), 'Translate class', $e->getMessage(), 'error');
        }
        return false;
    }

    public static function setLocale($locale) {

        $locale = is_string($locale) ? Hmf_Locale::createCoreInstanceLocale($locale) : $locale;

        if ($locale instanceof Zend_Locale and self::$translate instanceof Zend_Translate) {
            if (self::$translate->isAvailable($locale)) {
                self::$nowAvailable = true;
                self::$translate->setLocale($locale);
                return true;
            }
        }
        self::$nowAvailable = false;
        return false;
    }

    public static function getInstance() {

        return self::$translate;
    }

    /**
     * Translate string
     * @param $str
     * @return string
     */
    public static function _t($str) {

        if (self::$nowAvailable)
            return self::$translate->_($str);

        return $str;
    }

    /**
     * Print with translate string
     *
     * @param $str
     * @return null
     */
    public static function _e($str) {

        echo self::_t($str);
        return null;
    }

    /**
     * Formatting translation string
     *
     * @param string $s1 format for sprintf function
     * @param string $s2[,... $sN] unlimited number of additional argument for sprintf function
     * @return string
     */
    public static function _s() {

        $numArgs = func_num_args();
        if ($numArgs > 0) {
            $args = func_get_args();
            $args[0] = self::_t($args[0]);
            return call_user_func_array('sprintf', $args);
        }
        return null;
    }

    /**
     * Print translation and formatting string
     *
     * @param string $s1 format for sprintf function
     * @param mixed $s2[,...$argsN] unlimited number of additional variables to class _s method
     * @return null
     */
    public static function _p() {

        $numArgs = func_num_args();
        if ($numArgs > 0) {
            $args = func_get_args();
            echo call_user_func_array(array(self, '_s'), $args);
        }
        return null;
    }

   /**
     * Formatting translation string
     *
     * @param string $s format for sprintf function
     * @param array $a array
     * @return string
     */
    public static function _v() {

        $numArgs = func_num_args();
        if ($numArgs > 0) {
            $args = func_get_args();
            $args[0] = self::_t($args[0]);
            return call_user_func_array('vsprintf', $args);
        }
        return null;
    }
}

?>